#!/bin/bash

if [ "$1" = "--help" ];then
echo "Usage: $0 debpackage name. Then will create a taowa package at your path"
echo "Press enter to exit"
echo "I love Amber FOREVER!!!!!"
echo "用法： $0 包名。然后会在你的所在目录创建一个套娃包"
echo "按回车退出"
echo "我永远喜欢安柏！！！"
read
exit
fi

echo "Welcome to taowa package tool! Keep being horny can make you happy..."
echo
echo "Please use apt update to ensure you are using the newest version..."
echo
echo



package_name="$1"
work_dir=`mktemp -d`
cur_dir=`pwd`
taowa_script_install=`realpath $0`
taowa_script_install_dir=`dirname $taowa_script_install`
# 通过apt-cache查询包信息
package_info=$(apt-cache show "$package_name" 2>/dev/null)

# 解析版本号和包名
if [ $? -eq 0 ]; then
    version=$(echo "$package_info" | grep  "Version:" | head -n 1| awk '{print $2}')
    SIZE=$(echo "$package_info" | grep "Installed-Size:" | head -n 1| awk '{print $2}')
    echo "Package Name: $package_name"
    echo "Version: $version"
else
    echo "Package '$package_name' not found."
    exit 1
fi


echo "---------------------Now Start Create Package-------------------"
cd $work_dir
UOS_PACKAGE_NAME=store.spark-app.appwrapper.$package_name
UOS_PACKAGE_VERSION=$version


cd $work_dir
mkdir DEBIAN
mkdir -p opt/apps/$UOS_PACKAGE_NAME/entries/applications
mkdir -p opt/apps/$UOS_PACKAGE_NAME/entries/icons
mkdir -p opt/apps/$UOS_PACKAGE_NAME/files

echo "---------------------Writing control file....-------------------"
cat  << EOF >DEBIAN/control
Package: $UOS_PACKAGE_NAME
Version: $UOS_PACKAGE_VERSION
Architecture: all
Maintainer: shenmo <shenmo@spark-app.store>
Installed-Size: $SIZE
Depends: $package_name,zenity
Section: utils
Priority: extra
Description: Wrapper package of $package_name


EOF

echo "---------------------Writing launcher........--------------------"
cat << EOF > opt/apps/$UOS_PACKAGE_NAME/files/launcher.sh
#!/bin/bash
if [ "\$(which $package_name)" != "" ];then
$package_name "\$@"
exit
fi
zenity --info --icon-name=$UOS_PACKAGE_NAME --text="请在启动器（开始菜单）中启动我哦~\nPlease launch me in Launcher(Start menu)" --width 300

EOF

chmod +x opt/apps/$UOS_PACKAGE_NAME/files/launcher.sh

echo "---------------------Writing desktop.........--------------------"
cat << EOF > opt/apps/$UOS_PACKAGE_NAME/entries/applications/$UOS_PACKAGE_NAME.desktop
[Desktop Entry]
Categories=Utility;
Encoding=UTF-8
Exec=/opt/apps/$UOS_PACKAGE_NAME/files/launcher.sh
Icon=$UOS_PACKAGE_NAME
MimeType=
Name=$UOS_PACKAGE_NAME
StartupWMClass=$UOS_PACKAGE_NAME
Terminal=False
Type=Application
NoDisplay=true



EOF

echo "---------------------Copying Icon.............-------------------"
#TODO:extract the original package to get the icon
cp $taowa_script_install_dir/default-icon.svg opt/apps/$UOS_PACKAGE_NAME/entries/icons/$UOS_PACKAGE_NAME.svg


echo "---------------------Writing info file........-------------------"
cat  << EOF > opt/apps/$UOS_PACKAGE_NAME/info
{
    "appid": "$UOS_PACKAGE_NAME",
    "name": "$UOS_PACKAGE_NAME",
    "version": "$UOS_PACKAGE_VERSION",
    "arch": ["amd64"],
    "permissions": {
        "autostart": true,
        "notification": true,
        "trayicon": true,
        "clipboard": true,
        "account": true,
        "bluetooth": true,
        "camera": true,
        "audio_record": true,
        "installed_apps": true
    }
}

EOF

echo "---------------------Now make deb..............-------------------"
fakeroot dpkg-deb -Z xz -b . $cur_dir
cd $cur_dir
rm -rf $work_dir
